package ymy;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import ymy.BaseSimulator.StageResult;

public class LogUtil {

    public static String rfile = GeneralConfig.randFileName;
    public static String sfile = GeneralConfig.specFileName;

    public static PrintWriter rpw = null;
    public static PrintWriter spw = null;

    public static boolean initf = false;

    private LogUtil() { }

    public static void init() {
        System.out.println("Init Logger File Info.");
        FileWriter fw1 = null;
        BufferedWriter bw1 = null;
        try {
            fw1 = new FileWriter(rfile, true);
            bw1 = new BufferedWriter(fw1);
            rpw = new PrintWriter(bw1);
        } catch (IOException e) {
            e.printStackTrace();
            try {
                if (rpw != null) rpw.close();
                if (bw1 != null) bw1.close();
                if (fw1 != null) fw1.close();
            } catch (IOException ef) {
                ef.printStackTrace();
            }
        }

        FileWriter fw2 = null;
        BufferedWriter bw2 = null;
        try {
            fw2 = new FileWriter(sfile, true);
            bw2 = new BufferedWriter(fw2);
            spw = new PrintWriter(bw2);
        } catch (IOException e) {
            e.printStackTrace();
            try {
                if (spw != null) spw.close();
                if (bw2 != null) bw2.close();
                if (fw2 != null) fw2.close();
            } catch (IOException ef) {
                ef.printStackTrace();
            }
        }

        initf = true;
    }

    public static void writeLog(String s, boolean isrand) {
        if (!initf) init();

        if (isrand) {
            rpw.println(s);
            rpw.flush();
        } else {
            spw.println(s);
            spw.flush();
        }
    }

    public static void writeStageResultLog(StageResult sr, boolean isrand) {
        if (!initf) init();

        if (isrand) {
            rpw.printf("P-%d\tM-%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n",
                        sr.getParkSize(), sr.getMoveSize(), (double)sr.successCount / sr.totalCount,
                        sr.getSdelay(), sr.getSquality(), sr.getShops(), sr.getSdist(),
                        sr.getDelay(), sr.getQuality(), sr.getHops(), sr.getDist(),
                        sr.getAdelay(), sr.getAquality(), sr.getAhops(), sr.getAdist());
            rpw.flush();
        } else {
            spw.printf("P-%d\tM-%d\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\t%.3f\n",
                    sr.getParkSize(), sr.getMoveSize(), (double)sr.successCount / sr.totalCount,
                    sr.getSdelay(), sr.getSquality(), sr.getShops(), sr.getSdist(),
                    sr.getDelay(), sr.getQuality(), sr.getHops(), sr.getDist(),
                    sr.getAdelay(), sr.getAquality(), sr.getAhops(), sr.getAdist());
            spw.flush();
        }
    }
}
